//
//  LoginView.m
//  HappyBaby
//
//  Created by lanou on 15/11/30.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "LoginView.h"
#import "Define.h"

@interface LoginView ()



@end

@implementation LoginView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGFloat width = 280 * kScaleX;
        CGFloat height = 30*kScaleY;
        CGFloat x = (frame.size.width - width)/2;
        CGFloat y = 20 * kScaleY;
        
        CGFloat imageHeight = width * 556/1024;// 图片的大小为1024*640
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, imageHeight)];
        imageView.image = [UIImage imageNamed:@"placeHolder"];
        [self addSubview:imageView];
        
        
        UIImageView *leftimage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25*kScaleX, 25*kScaleY)];
        leftimage.image = [UIImage imageNamed:@"user"];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, height - 5 *kScaleY, width, 1*kScaleY)];
        lineView.backgroundColor = [UIColor  lightGrayColor];
        self.phoneOrEmail = [[UITextField alloc] initWithFrame:CGRectMake(x, imageView.y + imageView.height + 20 * kScaleY, width, height)];
        self.phoneOrEmail.placeholder = @" 手机号/邮箱";
        // 设置左缩进图
        self.phoneOrEmail.leftView = leftimage;
        // 模式为直接显示
        self.phoneOrEmail.leftViewMode = UITextFieldViewModeAlways;
        self.phoneOrEmail.delegate = self;
        [self.phoneOrEmail addSubview:lineView];
        [self addSubview:self.phoneOrEmail];
        
        
        UIImageView *leftimage2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20*kScaleX, 20*kScaleY )];
        leftimage2.image = [UIImage imageNamed:@"password"];
        UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(0, height - 5 *kScaleY, width, 1*kScaleY)];
        lineView2.backgroundColor = [UIColor  lightGrayColor];
        self.password = [[UITextField alloc] initWithFrame:CGRectMake(x, _phoneOrEmail.y + _phoneOrEmail.height + 20 * kScaleY, width, height)];
        self.password.secureTextEntry = YES;
        self.password.leftView = leftimage2;
        self.password.leftViewMode = UITextFieldViewModeAlways;
        self.password.delegate = self;
        self.password.placeholder = @" 密码";
        [self.password addSubview:lineView2];
        [self addSubview: self.password];
        
        self.loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.loginBtn.frame = CGRectMake(x, _password.y + _password.height + 30*kScaleY, width, height);
        self.loginBtn.backgroundColor = [UIColor colorWithRed:88/255.0 green:137/255.0 blue:236/255.0 alpha:1];
        [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [self.loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addSubview:_loginBtn];
        
        // 写一个label 装载注册和忘记密码
        UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, _loginBtn.y + _loginBtn.height + 10 *kScaleY , width, height)];
        buttonLabel.userInteractionEnabled = YES;
        buttonLabel.text = @"|";
        buttonLabel.textColor = [UIColor lightGrayColor];
        buttonLabel.textAlignment = UITextAlignmentCenter;
        [self addSubview:buttonLabel];
        
        
        // 创建注册button
        self.registBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        self.registBtn.frame = CGRectMake(0, 0, width/2*kScaleX, 30*kScaleY);
        [self.registBtn setTitle:@"用户注册" forState:UIControlStateNormal];
        [self.registBtn setTitleColor:[UIColor colorWithRed:88/255.0 green:137/255.0 blue:236/255.0 alpha:1] forState:UIControlStateNormal];
        [buttonLabel addSubview:_registBtn];
        
      
        // 创建忘记密码button
        self.forgetPasswordBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        self.forgetPasswordBtn.frame = CGRectMake(width/2, 0, width/2, 30);
        [self.forgetPasswordBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
        [self.forgetPasswordBtn setTitleColor:[UIColor colorWithRed:88/255.0 green:137/255.0 blue:236/255.0 alpha:1] forState:UIControlStateNormal];
        [buttonLabel addSubview:_forgetPasswordBtn];
        
        UILabel *buttomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.height - (49 + 64)*kScaleX, kScreenWidth, 49 * kScaleY)];
        buttomLabel.userInteractionEnabled = YES;
        buttomLabel.textAlignment = NSTextAlignmentCenter;
        buttomLabel.text = @"|";
        [self addSubview:buttomLabel];
        
        self.qqBtn = [DIYButton buttonWithType:UIButtonTypeCustom];
        self.qqBtn.frame = CGRectMake((buttomLabel.width/2 - 100*kScaleX)/2, (buttomLabel.height - height)/2, 100*kScaleX, height);
        self.qqBtn.iconImageView.image = [UIImage imageNamed:@"qqlogo_1"];
        self.qqBtn.textLabel.text = @"QQ登录";
        self.qqBtn.textLabel.textColor = [UIColor lightGrayColor];
        [buttomLabel addSubview:_qqBtn];
        
        self.sinaWeiboBtn = [DIYButton buttonWithType:UIButtonTypeCustom];
        self.sinaWeiboBtn.frame = CGRectMake(buttomLabel.width/2 + (buttomLabel.width/2 - 100*kScaleX)/2, _qqBtn.y, 100*kScaleX, height);
        self.sinaWeiboBtn.iconImageView.image = [UIImage imageNamed:@"sina1"];
        self.sinaWeiboBtn.textLabel.text = @"新浪登录";
        self.sinaWeiboBtn.textLabel.textColor = [UIColor lightGrayColor];
        [buttomLabel addSubview:_sinaWeiboBtn];
        
    }
    return self;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    UIView *lineView = textField.subviews[0];
    lineView.backgroundColor = [UIColor colorWithRed:88/255.0 green:137/255.0 blue:236/255.0 alpha:1];
    lineView.height = 2;
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    UIView *lineView = textField.subviews[0];
    lineView.height = 1;
    lineView.backgroundColor = [UIColor lightGrayColor];
    return YES;
}

// 退出键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.phoneOrEmail resignFirstResponder];
    [self.password resignFirstResponder];
}


@end
